package com.iostream;

import java.io.*;

/**
 * @Author WaveHi
 * @Date 20/8/2021 下午 5:11
 * @Version 1.0
 * @Contact HJBWaveHi@163.com
 * @Description Test the use of java.io.BufferedInputStream and java.io.BufferedOutputStream
 */
public class TestBufferedStream {

    public static void main(String[] args) {
        BufferedInputStream bis1 = null;
        BufferedOutputStream bos1 = null;

        try {
            //获取数据存取的位置
            File srcFile1 = new File("day11/txt/hello.txt");
            File destFile1 = new File("day11/txt/hello5.txt");

            //获取需存取的数据
            String ok = "Ok!";
            String oh = "Oh!";

            //获取流
            FileInputStream fis1 = new FileInputStream(srcFile1);
            FileOutputStream fos1 = new FileOutputStream(destFile1);
            bis1 = new BufferedInputStream(fis1);
            bos1 = new BufferedOutputStream(fos1);

            //数据的存取
            //接收存取的数据
            byte[] b = new byte[1024];

            //获取数据的长度
            int len = 0;

            //存取数据
            while((len = bis1.read(b)) != -1) {

                bos1.write(b,0,len);

            }
            System.out.println("Data transfer completed!");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //资源的关闭
            if(bis1 != null) {

                try {
                    bis1.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
            if(bos1 != null) {

                try {
                    bos1.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }


    }

}
